<%@ page pageEncoding="UTF-8"%>
<%@ include file="../commons/head.jsp"%>
<title>点位申请</title>
<style>
td {
	padding-top: 5px;
	padding-bottom: 5px;
	padding-right: 5px;
}

td:first-child {
	padding-left: 5px;
	padding-right: 0;
}

.error {
	color: red;
	font-style: normal;
	font-weight: bold;
}

.success {
	color: green;
	font-style: normal;
	font-weight: bold;
}
</style>
</head>
<script type="text/javascript">
var loginAccount = 'baidd';
var branchId;
$(function(){
		
	    $("#cellPhone").bind('keyup', function(e){
		        $("#cellPhone").val( $(this).val().replace(/\D/g,''));
	    });
	
	    $("#recommenderCellphone").bind('keyup', function(e){
		        $("#recommenderCellphone").val( $(this).val().replace(/\D/g,''));
	    });
	
	var flag=false;
	$("#cellPhone").blur(function(){
		$("#err").text("");
		$("#memberName").val("")
		$("#branchId").val("")
		$("#recommenderCellphone").val("")
		$("#recommenderName").val("")
		$("#recommenderCellphoneErr").text("");
		var tel = $("#cellPhone").val();
		if(tel ==null || tel.length == 0){
			return;
		}
		if(tel.length!=11 || isNaN(tel)){
			$("#err").text("输入手机号格式不对");
			return;
		}
		$.ajax({
			url:"${ctx}/delegatedauthority/memberinfo",
			cache:false,
			data:{cell:$("#cellPhone").val()},
			success:function(data){debugger
				if(data != null && data != ""){
					flag = true;
					var memberName =data.memberName;
					var userName =data.userName;
					branchId = data.branchId;
					$("#memberName").val(memberName);
					$("#branchId").val(userName);
					$.ajax({
						url:"${ctx}/delegatedauthority/isfirst",
						cache:false,
						data:{cell:$("#cellPhone").val()},
						success:function(data){debugger
							if(data != null && data != ""){
								$("#recommenderCellphone").val(data);
								$("#recommenderCellphone").attr("readonly",true);
								$.ajax({
									url:"${ctx}/delegatedauthority/memberinfo",
									cache:false,
									data:{cell:$("#recommenderCellphone").val()},
									success:function(data){debugger
										 $("#recommenderName").val(data.memberName);
										$("#recommenderName").attr("readonly",true);
									}
								});
							}else{
								$("#recommenderCellphone").attr("readonly",false);
								$("#recommenderCellphone").blur(aa);
							}
						}
						
					});
				}else{
					$("#err").text("该会员不存在或已被锁定");
					$("#memberName").val("")
					$("#branchId").val("")
					$("#recommenderCellphone").val("")
					$("#recommenderName").val("")
					return;
				}
			}
		});
	});
	
	
	var aa =function(){
		$("#recommenderCellphoneErr").text("");
		var tel = $("#recommenderCellphone").val();
		if(tel ==null || tel.length == 0){
			return;
		}
		if( tel.length!=11 || isNaN(tel)){
			$("#recommenderCellphoneErr").text("输入手机号格式不对");
			$("#recommenderName").val("")
			return;
		}
		$.ajax({
			url:"${ctx}/delegatedauthority/isagents",
			cache:false,
			data:{cell:$("#recommenderCellphone").val()},
			success:function(data){
				if(data != null && data != ""){
					if(data.branchId == branchId){
						$("#recommenderName").val(data.memberName);
						flag = true;
					}else{
						$("#recommenderCellphoneErr").text("推荐人与该会员所属开单中心不一致");
					}
				}else{
					$("#recommenderCellphoneErr").text("该推荐人还不是代理商");
					$("#recommenderName").val("")
					flag = false;
				}
			}
		});
	}
	$("#recommenderCellphone").attr("aa","bb").blur(aa);

	$("#nodeCount").change(function(){debugger
		var nodeCount = $("#nodeCount").val();
		$("#cash").val(nodeCount*1580);
	});

	function insert(){
		var tel = $("#cellPhone").val();
		var count = $("#nodeCount").val();
		if(tel=null || tel=="" ){
			$("#err").text("手机号格式不能为空");
			return;
		}
		$("#subm").attr("disabled",true);
		if(flag==true){
			$.ajax({
				url:"${ctx}/delegatedauthority/nodeorder",
				cache:false,
				data:{recommenderCellphone:$("#recommenderCellphone").val(),cellphone:$("#cellPhone").val()
					,nodeCount:$("#nodeCount").val(),cash:$("#cash").val(),branchId:branchId},
				success:function(data){debugger
					var count = parseInt($("#nodeCount").val());
					if(data==(1+count)){
						$.messager.alert("成功","点位购买成功","info");
						 $("#cellPhone").val("");
						 $("#memberName").val("");
						 $("#recommenderCellphone").val("");
						 $("#recommenderName").val("");
						 $("#cash").val("");
						 $("#branchId").val("");
						 $('#nodeCount').val("1");
						 $("#recommenderCellphone").prop("readonly",true);
						 $("#subm").attr("disabled",false);
					}else{
						$.messager.alert("失败：","点位订单生成失败","error");
						$("#subm").attr("disabled",false);
					}
				}
			});
		}
	};
	$("#subm").click(insert);

})

</script>

<body>
<div class="jianju">
<h3>代理权购买</h3>
	<div class="container-fluid">
		<form class="form-horizontal" method="post" id="form1" action="/dealershipApply/dealershipApply">
			<div class="form-group">
				<label for="cellPhone" class="col-md-3 control-label">会员手机号：</label>
				<div class="col-md-2">
				<input class="form-control" name="cellPhone" id="cellPhone"/>
				</div>
				<div class="col-md-3">
					<label class="error" id=err></label>
				</div>
			</div>
				
			<div class="form-group">
				<label for="memberName" class="col-md-3 control-label">会员姓名：</label>
				 <div class="col-md-2">
					<input class="form-control"  name="memberName" id="memberName" readonly />
				</div> 
			</div>
			
			<div class="form-group">
				<label for="branchId" class="col-md-3 control-label">开单中心：</label>
				 <div class="col-md-2">
					<input class="form-control"  name="branchId" id="branchId" readonly />
				</div> 
			</div>
			
			<div class="form-group">
				<label for="recommenderCellphone" class="col-md-3 control-label">推荐人手机号：</label>
				<div class="col-md-2">
				<input class="form-control" name="recommenderCellphone" id="recommenderCellphone" readonly/>
				</div>
				<div class="col-md-3">
					<label class="error" id="recommenderCellphoneErr"></label>
				</div>
			</div>
				
			<div class="form-group">
				<label for="recommenderName" class="col-md-3 control-label">推荐人姓名：</label>
				 <div class="col-md-2">
					<input class="form-control"  name="recommenderName" id="recommenderName" readonly/>
				</div> 
			</div>
			
				
			<div class="form-group">
			<label for=nodeCount class="col-md-3 control-label">购买数量：</label>
			 <div class="col-md-2">
			<select id="nodeCount" onSelect="sele" editable="false"  items="" class="form-control"   panelHeight="90" style="width:200px;height:35px" required="true">
				<option selected = "selected"  value="1" >1</option>
				<option value="2" >2</option>
				<option value="3" >3</option>
				<option value="4" >4</option>
			</select>
			</div>
			</div>
			
			<div class="form-group">
				<label for="cash" class="col-md-3 control-label">总金额：</label>
				 <div class="col-md-2">
					<input class="form-control"  readonly="true" name="cash" id="cash" value="1580"/>
				</div> 
			</div>
			<hr style="width: 98%;">
			<div class="form-group">
				<div class="col-md-3"></div>
				<div class="col-md-2"><button class="easyui-linkbutton" style="width:100px;height:30px"  iconCls="icon-ok"  id="subm"  type="button"  heightclass="btn btn-primary btn-lg" >保存</button></div>
			</div>
		</form>
	</div>
	</div>
</body>

</html>